/**
* Created with CLion.
* User:Lenovo
* Date:2023-06-29
* Time:16:56
* Description:
*/

#include "Sort.h"

int cmp(const void *e1, const void *e2)
{
    return *(int *)e1 - *(int *)e2;
}

void TestOP() // 测试性能 - 用 release 版本
{
    srand(time(0));
    const int N = 200000;
    int *a1 = (int *) malloc(sizeof(int) * N);
    int *a2 = (int *) malloc(sizeof(int) * N);
    int *a3 = (int *) malloc(sizeof(int) * N);
    int *a4 = (int *) malloc(sizeof(int) * N);
    int *a5 = (int *) malloc(sizeof(int) * N);
    int *a6 = (int *) malloc(sizeof(int) * N);

    for (int i = 0; i < N; ++i)
    {
        a1[i] = rand();
        a2[i] = a1[i];
        a3[i] = a1[i];
        a4[i] = a1[i];
        a5[i] = a1[i];
        a6[i] = a1[i];
    }

    int begin1 = clock();// 得到的是从进程开始到调用 clock 函数的时间，单位是 ms
    InsertSort(a1, N);
    int end1 = clock();

    int begin2 = clock();
    ShellSort(a2, N);
    int end2 = clock();

    int begin3 = clock();
    BubbleSort(a3, N);
    int end3 = clock();

    int begin4 = clock();
    SelectSort(a4, N);
    int end4 = clock();

    int begin5 = clock();
    HeapSort(a5, N);
    int end5 = clock();

    int begin6 = clock();
    qsort(a6, N, 4, cmp);
    int end6 = clock();

    printf("InsertSort:%d\n", end1 - begin1);
    printf("ShellSort:%d\n", end2 - begin2);
    printf("BubbleSort:%d\n", end3 - begin3);
    printf("SelectSort:%d\n", end4 - begin4);
    printf("HeapSort:%d\n", end5 - begin5);
    printf("QuickSort:%d\n", end6 - begin6);

    free(a1);
    free(a2);
    free(a3);
    free(a4);
    free(a5);
    free(a6);
}

void SortTest1()
{
    int a[] = {9, 0, 7, 5, 6, 4, 8, 3, 1, 2};
    int sz = sizeof(a) / sizeof(a[0]);

    PrintArr( a, sz);
    InsertSort(a, sz);
    PrintArr(a, sz);
}

void SortTest2()
{
    int a[] = {9, 0, 7, 5, 6, 4, 8, 3, 1, 2};
    int sz = sizeof(a) / sizeof(a[0]);

    PrintArr( a, sz);
    ShellSort(a, sz);
    PrintArr(a, sz);
}

void SortTest3()
{
    int a[] = {9, 0, 7, 5, 6, 4, 8, 3, 1, 2};
    int sz = sizeof(a) / sizeof(a[0]);

    PrintArr( a, sz);
    BubbleSort(a, sz);
    PrintArr(a, sz);
}

void SortTest4()
{
    int a[] = {9, 0, 7, 5, 6, 4, 8, 3, 1, 2};
    int sz = sizeof(a) / sizeof(a[0]);

    PrintArr( a, sz);
    SelectSort(a, sz);
    PrintArr(a, sz);
}

int main()
{
    SortTest1();
    SortTest2();
    SortTest3();
    SortTest4();
    TestOP();
}
